using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace dev173d
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        BasicPrimitive strangeObject;
        BasicPrimitive[] objetos;

        int rotacaoX = 0;
        int rotacaoY = 0;
        int rotacaoZ = 0;

        KeyboardState teclado_atual;

        BasicCamera camera;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            objetos = new BasicPrimitive[5];

            objetos[0] = new BasicPrimitive(GraphicsDevice, Content.Load<Texture2D>("parede"), Color.White);
            objetos[1] = new BasicPrimitive(GraphicsDevice, Content.Load<Texture2D>("teto"), Color.White);
            objetos[2] = new BasicPrimitive(GraphicsDevice, Content.Load<Texture2D>("parede"), Color.White);
            objetos[3] = new BasicPrimitive(GraphicsDevice, Content.Load<Texture2D>("chao"), Color.White);
            objetos[4] = new BasicPrimitive(GraphicsDevice, Content.Load<Texture2D>("parede"), Color.White);


            objetos[0].World = Matrix.CreateScale(2) * Matrix.CreateTranslation(0, 0, 0);
            objetos[1].World = Matrix.CreateRotationX(MathHelper.ToRadians(-90))*Matrix.CreateScale(4)*Matrix.CreateTranslation(0, -2, 2);
            objetos[2].World = Matrix.CreateScale(2) * Matrix.CreateTranslation(-2, 0, 0);
            objetos[3].World = Matrix.CreateRotationX(MathHelper.ToRadians(90)) * Matrix.CreateScale(4) * Matrix.CreateTranslation(0, 2, 2);
            objetos[4].World = Matrix.CreateScale(2) * Matrix.CreateTranslation(2, 0, 0);
            

            //objetos[1]


            
            camera = new BasicCamera(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            teclado_atual = Keyboard.GetState();

            camera.Update(gameTime);

            objetos[0].Update(gameTime);
            objetos[1].Update(gameTime);
            objetos[2].Update(gameTime);
            objetos[3].Update(gameTime);
            objetos[4].Update(gameTime);

            if (teclado_atual.IsKeyDown(Keys.W))
            {
                camera.Target = new Vector3(0, 2, 2);
            }

            if (teclado_atual.IsKeyDown(Keys.S))
            {
                camera.Target = new Vector3(0, -2, 2);
            }
            if (teclado_atual.IsKeyDown(Keys.A))
            {
                camera.Target = new Vector3(2, 0, 0);
            }
            if (teclado_atual.IsKeyDown(Keys.D))
            {
                camera.Target = new Vector3(-2 , 0 , 0);
            }
            if (teclado_atual.IsKeyDown(Keys.Q))
            {
                camera.Target = new Vector3(0, 0, 0);
            }

            if (teclado_atual.IsKeyDown(Keys.Home))
            {
                rotacaoX += 10;
            }
            if (teclado_atual.IsKeyDown(Keys.End))
            {
                rotacaoX -= 10;
            }
            if (teclado_atual.IsKeyDown(Keys.Delete))
            {
                rotacaoY -= 10;
            }
            if (teclado_atual.IsKeyDown(Keys.PageDown))
            {
                rotacaoY += 10;
            }
            if (teclado_atual.IsKeyDown(Keys.Insert))
            {
                rotacaoZ -= 10;
            }
            if (teclado_atual.IsKeyDown(Keys.PageUp))
            {
                rotacaoZ += 10;
            }

            objetos[0].World = Matrix.CreateRotationX(MathHelper.ToRadians(rotacaoX)) * Matrix.CreateRotationY(MathHelper.ToRadians(rotacaoY)) * Matrix.CreateRotationZ(MathHelper.ToRadians(rotacaoZ));
            


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            objetos[0].Draw(gameTime, camera);
            objetos[1].Draw(gameTime, camera);
            objetos[2].Draw(gameTime, camera);
            objetos[3].Draw(gameTime, camera);
            objetos[4].Draw(gameTime, camera);
            
            base.Draw(gameTime);
        }
    }
}
